/*
@Time : 2021/2/7 11:22
@Author : 吴周海
@Mail: wzh200x@126.com
@File : DBModel.go
@Software: GoLand
*/
package models

import (
	"GameApiServer/requestData"
	"GameApiServer/utils"
	"github.com/beego/beego/v2/client/orm"
	"github.com/beego/beego/v2/core/logs"
	"strconv"
	"time"
)

func init() {
	orm.RegisterModel(new(GameServerInfo), new(PayRecordInfo))
}

// 服务器信息配置表
type GameServerInfo struct {
	ServerId          int       `orm:"column(serverId);pk"`       // 服务器ID
	IpHost            string    `orm:"column(ip)"`                // 地址
	MasterPort        string    `orm:"column(masterPort)"`        // 管理端口
	OpenTime          time.Time `orm:"column(openTime)"`          // 开启时间
	ChannelOperations string    `orm:"column(channelOperations)"` // 渠道
	ChannelId         int       `orm:"column(channelId)"`         // 渠道ID
}

func (i *GameServerInfo) TableName() string {
	return "gServersInfo"
}

func (i *GameServerInfo) Read() error {
	o := orm.NewOrm()
	err := o.Read(i)
	if err != nil {
		logs.Error("Can not find server", i.ServerId)
	}
	return err
}

// 充值记录表
type PayRecordInfo struct {
	Id           int    `orm:"column(id)"`         // 自增长ID
	Uid          string `orm:"column(uid)"`        // 	用户ID
	NickName     string `orm:"column(nickname)"`   // 	昵称
	Order        string `orm:"column(oid)"`        // 	订单号
	GoodsId      string `orm:"column(goodsId)"`    // 	商品ID
	PayTime      string `orm:"column(pTime)"`      // 	支付时间
	Sid          string `orm:"column(sid)"`        // 	服务器ID
	Channel      string `orm:"column(channel)"`    //	渠道ID
	PayMoney     string `orm:"column(pMoney)"`     //  付款数目单位:分
	ExtData      string `orm:"column(ext)"`        // 	扩展数据
	RequestTime  string `orm:"column(rTime)"`      //	请求时间
	IsPaySuccess string `orm:"column(paySuccess)"` //  是否支付成功
	Sign         string `orm:"column(sign)"`       // 	签名
	Ip           string `orm:"column(ip)"`         //  请求来源IP
	IsSuccess    int    `orm:"column(success)"`    //  充值成功
	RepeatNotice int    `orm:"column(rNotice)"`    //  重复通知次数
}

func (i *PayRecordInfo) TableName() string {
	return "payRecordInfo"
}

func (i *PayRecordInfo) CheckIsExist() bool {
	if i.ReadOrCreate() {
		return true
	}
	return false
}

func (i *PayRecordInfo) UpdatePayStatus() {
	i.IsSuccess = 1
	i.Update()
}

func (i *PayRecordInfo) ReadOrCreate() bool {
	o := orm.NewOrm()
	// 三个返回参数依次为：是否新创建的，对象 Id 值，错误
	if created, _, err := o.ReadOrCreate(i, "Order"); err == nil {
		if created {
			return false
		}
	}
	return true
}

func (i *PayRecordInfo) Update() {
	o := orm.NewOrm()
	payInfo := PayRecordInfo{Id: i.Id}
	if o.Read(&payInfo) == nil {
		o.Update(i)
	}
}

// 查找失败的订单
func FindNoSuceessOrder() []*PayRecordInfo {
	o := orm.NewOrm()
	var orders []*PayRecordInfo
	num, err := o.QueryTable(&PayRecordInfo{}).
		Filter("success", 0).
		Filter("rNotice__lt", 5).
		Filter("paySuccess", "0").
		All(&orders)
	if err == nil {
		if num > 0 {

		}
	} else {
		logs.Error("FindNoSuceessOrder err:", err)
	}
	return orders
}

func NewPayRecordInfo(payData *requestData.PayBackData) *PayRecordInfo {
	return &PayRecordInfo{
		Uid:          payData.Uid,
		NickName:     payData.NickName,
		Order:        payData.Order,
		GoodsId:      payData.GoodsId,
		PayTime:      payData.PayTime,
		Sid:          payData.Sid,
		Channel:      payData.Channel,
		PayMoney:     payData.PayMoney,
		ExtData:      payData.ExtData,
		RequestTime:  payData.RequestTime,
		Sign:         payData.Sign,
		Ip:           "",
		IsSuccess:    0,
		RepeatNotice: 0,
		IsPaySuccess: payData.IsPaySuccess,
	}
}

// 这里有一个小变动,因为乐堂自身的UId不能作为游戏的账号,只能使用玩家自己的账号或是平台UID来作为游戏账号,所以跟
// 前端协商之后,决定在扩展数据中传输真实游戏UID,而此处的UID参数实际是乐堂网络的用户UID
func LeTangNewPayRecordInfo(payData *requestData.LeTangPayBackData) *PayRecordInfo {
	_, timeStr := utils.GetFormatTime()
	re := &PayRecordInfo{
		Uid:          payData.ExtData,
		NickName:     "",
		Order:        payData.Order,
		GoodsId:      payData.GoodsId,
		PayTime:      "",
		Sid:          strconv.Itoa(payData.Sid),
		Channel:      "乐堂",
		PayMoney:     strconv.Itoa(payData.PayMoney),
		ExtData:      payData.UserId,
		RequestTime:  timeStr,
		Sign:         "",
		Ip:           "",
		IsSuccess:    0,
		RepeatNotice: 0,
		IsPaySuccess: payData.Code,
	}
	return re
}
